#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <sensor_msgs/LaserScan.h>

ros::Publisher pub;
void lidar_callback(sensor_msgs::LaserScan msgs)
{
  float fmidist = msgs.ranges[180];
  ROS_INFO("前方测距 ranges[180] = %f 米", fmidist);
 geometry_msgs::Twist msg;
  if (fmidist < 1.5)
  {
    msg.angular.z = 10;
    int n  = 10 ;
    while (n--);
  }
  else
  {
    msg.linear.x = 0.5;
  }
  pub.publish(msg);
}
int main(int argc, char *argv[])
{
setlocale(LC_CTYPE,"zh_CN.utf8");
 ros::init(argc, argv, "ldc_node");
 ros::NodeHandle nh;
 pub = nh.advertise<geometry_msgs::Twist>("/cmd_vel",10);
 ros::Subscriber sub=  nh.subscribe("/scan", 10, &lidar_callback);
 ros::spin();
    return 0;
}